﻿Imports System
Imports System.Web
Imports dooll.Config

Friend Class RewriterUtils

    Friend Shared Sub RewriteUrl(ByVal context As HttpContext, ByVal sendToUrl As String)
        Dim x As String = String.Empty, y As String = String.Empty
        RewriteUrl(context, sendToUrl, x, y)
    End Sub

    Friend Shared Sub RewriteUrl(ByVal context As HttpContext, ByVal sendToUrl As String, ByRef sendToUrlLessQString As String, ByRef filePath As String)
        If sendToUrl.StartsWith("http://") Then
            context.Response.Redirect(sendToUrl)
            Return
        End If
        If (context.Request.QueryString.Count > 0) Then

            If (sendToUrl.IndexOf("?") > -1) Then
                sendToUrl += "&" + context.Request.QueryString.ToString()
            Else
                sendToUrl += "?" + context.Request.QueryString.ToString()
            End If
        End If

        Dim queryString As String = String.Empty

        sendToUrlLessQString = sendToUrl
        If (sendToUrl.IndexOf("?") > 0) Then
            sendToUrlLessQString = sendToUrl.Substring(0, sendToUrl.IndexOf("?"))
            queryString = sendToUrl.Substring(sendToUrl.IndexOf("?") + 1)
        End If

        filePath = String.Empty
        filePath = context.Server.MapPath(sendToUrlLessQString)
        context.Trace.Write("RewriterUtils", String.Concat("Rewriting URL to", sendToUrlLessQString))

        context.RewritePath(sendToUrlLessQString, String.Empty, queryString)

    End Sub

End Class

